// * auth component

import React from "react";
import { searchRoute } from "./search";
import { useLocation, useNavigate, Navigate } from "react-router-dom";
import { useUserStore } from "@/store";
import { message } from "antd";

/**
 * @description 导航守卫
 * @param props
 * @returns
 */
const AuthRouter = (props: { children: JSX.Element }) => {
  const { pathname } = useLocation();

  const token = useUserStore((state) => {
    return state.token;
  });

  let ele: JSX.Element = props.children;

  if (!token && pathname !== "/" && pathname !== "/login") {
    ele = <Navigate to={"/login"} />;
    message.warning("登录已过期，请重新登录!");
  }
  return ele;
};

export default AuthRouter;
